import time
import pickle
import win32api
import json
from docxtpl import DocxTemplate

def dtpl(old_name,new_name,datas):
    '''
    docx文档替换
    '''
    try:
        doc = DocxTemplate(old_name)
        doc.render(datas)
        doc.save(new_name)
    except Exception as e:
        print(e)

def today(d=0):
    return time.strftime('%Y-%m-%d', time.localtime(time.time()+60*60*24*d))

def today_str(d=0):
    return time.strftime('%Y%m%d', time.localtime(time.time()+60*60*24*d))

def now():
    return time.strftime('%Y-%m-%d %H:%M:%S')

def now_str():
    return time.strftime('%Y%m%d%H%M%S')


def j_save(filename, datas):
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(datas, f, ensure_ascii=False, indent=2)


def j_load(filename):
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            datas = json.load(f)
    except Exception as e:
        print('读取失败：', e)
        datas = None
    finally:
        return datas


def file_save(filename, datas):
    with open(filename, 'wb') as f:
        pickle.dump(datas, f)


def file_load(filename):
    try:
        with open(filename, 'rb') as f:
            datas = pickle.load(f)
    except Exception as e:
        print('读取失败：', e)
        datas = None
    finally:
        return datas


def print_file(filename):
    win32api.ShellExecute(0, "print", filename, None, ".", 0)